package com.qingke.easyjava.jdbc.app;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.qingke.easyjava.jdbc.dao.AnswerDao;
import com.qingke.easyjava.jdbc.dao.PlayerDao;
import com.qingke.easyjava.jdbc.dao.QuestionDao;
import com.qingke.easyjava.jdbc.pojo.Answer;
import com.qingke.easyjava.jdbc.pojo.Player;
import com.qingke.easyjava.jdbc.pojo.Question;


//访问数据库层
public class BeSuperDao {
	public BeSuperDao() {
		
	}
	
	public static SqlSession getSession() {
		SqlSession session = null;
		Reader reader;
		try {
			reader = Resources.getResourceAsReader("mybatisConfig.xml");
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
			session = sqlSessionFactory.openSession();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return session;
	}
	
	public void signup(String username,String password,String nickname) {
		SqlSession session = getSession();
		PlayerDao playerDao = session.getMapper(PlayerDao.class);
		playerDao.signup(username, password, nickname);
		session.commit();
		session.close();
	}
	
	public Player login(String username,String password) {
		SqlSession session = getSession();
		PlayerDao playerDao = session.getMapper(PlayerDao.class);
		Player player = playerDao.login(username, password);
		session.close();
		return player;
	}
	
	public Player getPlayer(long playerId) {
		SqlSession session = getSession();
		PlayerDao playerDao = session.getMapper(PlayerDao.class);
		Player player = playerDao.getPlayerById(playerId);
		session.close();
		return player;
	}
	
	
	public List<Question> getQuestionFrom(Player player) {//根据player信息查问题
		SqlSession session = getSession();
		QuestionDao questionDao = session.getMapper(QuestionDao.class);
		List<Question> questions = questionDao.getQuestionFrom(player);
		return questions;
	}
	
	 public Question getQuestion(long questionId) {
		 SqlSession session = getSession();
		 QuestionDao questionDao = session.getMapper(QuestionDao.class);
		 Question question = questionDao.getQuestion(questionId);
		 AnswerDao answerDao = session.getMapper(AnswerDao.class);
		 List<Answer> answers = answerDao.getAnswersFrom(question);
		 question.setAnswers(answers);
		 return question;
	    }
	
	public List<Question> getQuestions() {
		SqlSession session = getSession();
		QuestionDao questionDao = session.getMapper(QuestionDao.class);
		List<Question> questions = questionDao.getQuestions();
		return questions;
	}
	
	 public Answer getAnswer(long answerId) {
		  SqlSession session = getSession();
		  AnswerDao answerDao = session.getMapper(AnswerDao.class);
		  Answer answer = answerDao.getAnswer(answerId);
	      return answer;
	    }
	
	public List<Answer> getAnswersFrom(Player player) {
		SqlSession session = getSession();
		AnswerDao answerDao = session.getMapper(AnswerDao.class);
		List<Answer> answers = answerDao.getAnswersFrom(player);
		return answers;
	}
	
	public List<Answer> getAnswersFrom(Question question) {
		SqlSession session = getSession();
		AnswerDao answerDao = session.getMapper(AnswerDao.class);
		List<Answer> answers = answerDao.getAnswersFrom(question);
		return answers;
	}
	
	public void upsertQuestion(Question question) {
		SqlSession session = getSession();
		QuestionDao questionDao = session.getMapper(QuestionDao.class);
		questionDao.upsertQuestion(question);
		session.commit();
	}
	
	public void upsertAnswer(Answer answer) {
		SqlSession session = getSession();
		AnswerDao answerDao = session.getMapper(AnswerDao.class);
		answerDao.upsertAnswer(answer);
		session.commit();
	}
	
	public boolean isUsernameExists(String username) {
		SqlSession session = getSession();
		PlayerDao playerDao = session.getMapper(PlayerDao.class);
		String isExist = playerDao.isUsernameExists(username);
		if(isExist != null) {
			return true;
		}
		return false;
	}
	
	public void acceptAnswer1(Answer answer) {
		SqlSession session = getSession();
		AnswerDao answerDao = session.getMapper(AnswerDao.class);
		answerDao.acceptAnswer1(answer);
		session.commit();
    }
	
	public void acceptAnswer2(Answer answer) {
		SqlSession session = getSession();
		QuestionDao questionDao = session.getMapper(QuestionDao.class);
		questionDao.acceptAnswer2(answer.getQuestionId());
		session.commit();
    }

}
